/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uq.comp7705.servlet;

import edu.uq.comp7705.entity.Person;
import edu.uq.comp7705.entity.Session;
import edu.uq.comp7705.entity.SessionDrink;
import edu.uq.comp7705.util.UtilDB;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author javierpanchi
 */
public class General extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String path = "/";
        try {
            UtilDB utilDB = new UtilDB();
            if (request.getParameter("opt") != null && request.getParameter("opt").equalsIgnoreCase("save")) {
                Person person = new Person(request.getParameter("name"),
                        request.getParameter("address"),
                        request.getParameter("mobile"),
                        Double.parseDouble(request.getParameter("weight")),
                        Double.parseDouble(request.getParameter("height")),
                        Integer.parseInt(request.getParameter("age")),
                        request.getParameter("gender"),
                        request.getParameter("emergency"),
                        request.getParameter("idFacebook"));
                Person p=utilDB.savePerson(person);
                request.getSession().setAttribute("person", p);
                path = "/jsp/session.jsp";
                //request.setAttribute("message", "New profile registred");

            } else if((request.getParameter("opt") != null && request.getParameter("opt").equalsIgnoreCase("friends"))){
                path = "/jsp/session.jsp";
            }else if (request.getParameter("opt") != null && request.getParameter("opt").equalsIgnoreCase("session")) {
                List<Session> sessionDrink=new ArrayList<Session>();
                if(request.getParameterValues("idFacebook")!=null){
                for(String idFacebook:request.getParameterValues("idFacebook")){
                    Session session=new Session();
                    session.setDescription(request.getParameter("description"));
                    session.setPlace(request.getParameter("place"));
                    session.setDrkPersonId((Person)request.getSession().getAttribute("person"));
                    session.setSince(new Date());
                    session.setFacebookid(idFacebook.split("\\|")[0]);
                    session.setNameFacebook(idFacebook.split("\\|")[1]);
                    session.setClosed(0);
                    sessionDrink.add(session);
                    int idSession=utilDB.saveSession(session);
                    request.getSession().setAttribute(session.getFacebookid(), idSession);
                }
                
                request.getSession().setAttribute("sessionDrink", sessionDrink);
                request.setAttribute("message", "Session registred");
                }
                path="/jsp/drinks.jsp";
            } else if (request.getParameter("opt") != null && request.getParameter("opt").equalsIgnoreCase("add")) {
                SessionDrink sessionDrink=new SessionDrink();
                Integer idSession=new Integer(request.getSession().getAttribute(request.getParameter("facebookId")).toString());//Aqui esta el amage con Facebook id y la session
                Integer idDrink=new Integer(request.getParameter("drink"));
                
                utilDB.updateDrinkSession(idDrink,idSession);
            }
            
            else {
                request.setAttribute("message", "Option not available");
            }
            RequestDispatcher rd = request.getRequestDispatcher(path);
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
